import 'package:get_it/get_it.dart';
import 'package:redux/redux.dart';
import '../store/redux_user_manager.dart';
import '../store/redux_chat_manager.dart';
import '../store/redux_follow_manager.dart';
import '../store/redux_social_manager.dart';
import '../store/redux_qa_manager.dart';
import '../store/redux_leaderboard_manager.dart';
import '../store/redux_visitor_manager.dart';

/// 组合应用状态
class AppState {
  final ReduxUserState userState;
  final ReduxChatState chatState;
  final ReduxFollowState followState;
  final ReduxSocialState socialState;
  final ReduxQAState qaState;
  final ReduxLeaderboardState leaderboardState;
  final ReduxVisitorState visitorState;

  const AppState({
    required this.userState,
    required this.chatState,
    required this.followState,
    required this.socialState,
    required this.qaState,
    required this.leaderboardState,
    required this.visitorState,
  });

  AppState copyWith({
    ReduxUserState? userState,
    ReduxChatState? chatState,
    ReduxFollowState? followState,
    ReduxSocialState? socialState,
    ReduxQAState? qaState,
    ReduxLeaderboardState? leaderboardState,
    ReduxVisitorState? visitorState,
  }) {
    return AppState(
      userState: userState ?? this.userState,
      chatState: chatState ?? this.chatState,
      followState: followState ?? this.followState,
      socialState: socialState ?? this.socialState,
      qaState: qaState ?? this.qaState,
      leaderboardState: leaderboardState ?? this.leaderboardState,
      visitorState: visitorState ?? this.visitorState,
    );
  }
}

/// 组合应用Reducer
AppState appReducer(AppState state, dynamic action) {
  return AppState(
    userState: userReducer(state.userState, action),
    chatState: chatReducer(state.chatState, action),
    followState: followReducer(state.followState, action),
    socialState: socialReducer(state.socialState, action),
    qaState: qaReducer(state.qaState, action),
    leaderboardState: leaderboardReducer(state.leaderboardState, action),
    visitorState: visitorReducer(state.visitorState, action),
  );
}

/// 服务定位器
/// 使用GetIt进行依赖注入管理
final GetIt serviceLocator = GetIt.instance;

/// 初始化所有服务
Future<void> initializeAllServices() async {
  // 创建组合Redux Store
  final store = Store<AppState>(
    appReducer,
    initialState: const AppState(
      userState: ReduxUserState(),
      chatState: ReduxChatState(),
      followState: ReduxFollowState(),
      socialState: ReduxSocialState(),
      qaState: ReduxQAState(),
      leaderboardState: ReduxLeaderboardState(),
      visitorState: ReduxVisitorState(),
    ),
  );

  // 注册Store
  serviceLocator.registerSingleton<Store<AppState>>(store);

  // 注册用户管理器
  serviceLocator.registerSingleton<ReduxUserManager>(ReduxUserManager());

  // 注册聊天管理器
  serviceLocator.registerSingleton<ReduxChatManager>(ReduxChatManager());

  // 注册关注管理器
  serviceLocator.registerSingleton<ReduxFollowManager>(ReduxFollowManager());

  // 注册社交动态管理器
  serviceLocator.registerSingleton<ReduxSocialManager>(ReduxSocialManager());

  // 注册问答管理器
  serviceLocator.registerSingleton<ReduxQAManager>(ReduxQAManager());

  // 注册排行榜管理器
  serviceLocator.registerSingleton<ReduxLeaderboardManager>(
    ReduxLeaderboardManager(),
  );

  // 注册访客管理器
  serviceLocator.registerSingleton<ReduxVisitorManager>(ReduxVisitorManager());

  // 初始化用户管理器
  final userManager = serviceLocator<ReduxUserManager>();
  userManager.initialize(store);
  userManager.initializeUserData();

  // 初始化聊天管理器
  final chatManager = serviceLocator<ReduxChatManager>();
  chatManager.initialize(store);
  chatManager.initializeChatData();

  // 初始化关注管理器
  final followManager = serviceLocator<ReduxFollowManager>();
  followManager.initialize(store);
  followManager.initializeFollowData();

  // 初始化社交动态管理器
  final socialManager = serviceLocator<ReduxSocialManager>();
  socialManager.initialize(store);
  socialManager.initializeSocialData();

  // 初始化问答管理器
  final qaManager = serviceLocator<ReduxQAManager>();
  qaManager.initialize(store);
  qaManager.initializeQAData();

  // 初始化排行榜管理器
  final leaderboardManager = serviceLocator<ReduxLeaderboardManager>();
  leaderboardManager.initialize(store);
  leaderboardManager.initializeLeaderboardData();

  // 初始化访客管理器
  final visitorManager = serviceLocator<ReduxVisitorManager>();
  visitorManager.initialize(store);
  visitorManager.initializeVisitorData();
}

/// 获取用户管理器实例
ReduxUserManager get userManager => serviceLocator<ReduxUserManager>();

/// 获取聊天管理器实例
ReduxChatManager get chatManager => serviceLocator<ReduxChatManager>();

/// 获取关注管理器实例
ReduxFollowManager get followManager => serviceLocator<ReduxFollowManager>();

/// 获取社交动态管理器实例
ReduxSocialManager get socialManager => serviceLocator<ReduxSocialManager>();

/// 获取问答管理器实例
ReduxQAManager get qaManager => serviceLocator<ReduxQAManager>();

/// 获取排行榜管理器实例
ReduxLeaderboardManager get leaderboardManager =>
    serviceLocator<ReduxLeaderboardManager>();

/// 获取应用Store实例
Store<AppState> get appStore => serviceLocator<Store<AppState>>();

/// 获取用户Store实例（兼容性）
/// 注意：返回的是组合Store，需要访问.userState来获取用户状态
Store<AppState> get userStore => appStore;

/// 获取聊天Store实例
/// 注意：返回的是组合Store，需要访问.chatState来获取聊天状态
Store<AppState> get chatStore => appStore;

/// 获取关注Store实例
/// 注意：返回的是组合Store，需要访问.followState来获取关注状态
Store<AppState> get followStore => appStore;

/// 获取社交动态Store实例
/// 注意：返回的是组合Store，需要访问.socialState来获取社交动态状态
Store<AppState> get socialStore => appStore;

/// 获取问答Store实例
/// 注意：返回的是组合Store，需要访问.qaState来获取问答状态
Store<AppState> get qaStore => appStore;

/// 获取排行榜Store实例
/// 注意：返回的是组合Store，需要访问.leaderboardState来获取排行榜状态
Store<AppState> get leaderboardStore => appStore;

/// 获取访客管理器实例
ReduxVisitorManager get visitorManager => serviceLocator<ReduxVisitorManager>();

/// 获取访客Store实例
/// 注意：返回的是组合Store，需要访问.visitorState来获取访客状态
Store<AppState> get visitorStore => appStore;
